/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import who.tmap.bo.BizObjects;
import who.tmap.bo.TextStyle;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.common.TMapException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.annotation.LabelsListShape;
import who.tmap.ui.util.JDialogFontChooser;
/**
 * @author stl
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JDialogLabelsList extends JDialog {

    private static Logger logger = Logger.getLogger(JDialogLabelsList.class);	
	
	private TextStyle style;
	
	private List<LabelsListShape> labelList;
	LabelsListShape labelsListShape;
	
	private javax.swing.JPanel jContentPane = null;
	private JPanel jPanelName = null;
	private JLabel jLabelListName = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	
	private JTextField jTextFieldLabelListName = null;
	private JCheckBox jCheckBoxTransparentBackground = null;
	private JButton jButtonStyle = null;
	
	private boolean okPressed =false;

	private JCheckBox jCheckBoxVisible = null;
	
	/**
	 * This is the default constructor
	 */
	public JDialogLabelsList( Frame parent, boolean modal, List<MapShape> shapes, LabelsListShape labelsListShape ) {
		super( parent, modal );
		
		labelList =new ArrayList<LabelsListShape>();
		if ( shapes != null ) {
			for( MapShape shape : shapes ) {
				if ( shape instanceof LabelsListShape ) {
					labelList.add( (LabelsListShape)shape );
				}
			}
		}
		
		this.labelsListShape =labelsListShape;
		
		initialize();
		
		if ( labelsListShape != null ) {
			getJTextFieldLabelListName().setText( labelsListShape.getName() );
			getJCheckBoxTransparentBackground().setSelected( labelsListShape.isBackgroundTransparent() );
			getJCheckBoxVisible().setSelected( labelsListShape.isVisible() );
			
			style =labelsListShape.getStyle();
		} else {
			getJTextFieldLabelListName().setText( "Untitled" );
			getJCheckBoxTransparentBackground().setSelected( false );
			getJCheckBoxVisible().setSelected( true );
			
			try {
				style =(TextStyle)BizObjectFactory.getInstance().get( BizObjects.TEXT_STYLE );
			} catch (TMapException e) {
				logger.error( e );
			}
		}

		//setAlwaysOnTop( modal );
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Label List");

		this.setBounds(new java.awt.Rectangle(0, 0, 400,200));
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
			jContentPane.add(getJPanelName(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJPanelButtons(), java.awt.BorderLayout.SOUTH);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelName() {
		if (jPanelName == null) {
			GridLayout gridLayout = new GridLayout(3,2,5,5);
			jLabelListName = new JLabel();
			jPanelName = new JPanel();
			jLabelListName.setText("Label list's name");
			jPanelName.setLayout(gridLayout);
			jPanelName.add(jLabelListName, null);
			jPanelName.add(getJTextFieldLabelListName(), null);
			jPanelName.add(getJCheckBoxVisible(), null);
			jPanelName.add(getJButtonStyle(), null);
			jPanelName.add(getJCheckBoxTransparentBackground(), null);				
		}
		return jPanelName;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			//jButtonOK.setSelected(true);
			jButtonOK.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					if ( labelsListShape != null ) {
						labelsListShape.setName( getJTextFieldLabelListName().getText() );
						labelsListShape.setStyle( getStyle() );
						labelsListShape.setBackgroundTransparent( getJCheckBoxTransparentBackground().isSelected() );
						labelsListShape.setVisible( getJCheckBoxVisible().isSelected() );
					}
					
					for( LabelsListShape lls : labelList ) {
						if ( ( labelsListShape == null || labelsListShape != lls )  
								&& lls.getName().equalsIgnoreCase( getJTextFieldLabelListName().getText() ) ) {
							JOptionPane.showMessageDialog(null,
						            "This name conflicts with another labels list name",
						            "Labels list name conflicts",
						            JOptionPane.ERROR_MESSAGE); 
							return;
						}
					}
					
					okPressed =true;
					setVisible( false );
				}
			});
		}
		return jButtonOK;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jTextFieldPolygonName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getJTextFieldLabelListName() {
		if (jTextFieldLabelListName == null) {
			jTextFieldLabelListName = new JTextField();
		}
		return jTextFieldLabelListName;
	}
	
	/**
	 * This method initializes jCheckBoxPolygonNameVisible	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	public JCheckBox getJCheckBoxTransparentBackground() {
		if (jCheckBoxTransparentBackground == null) {
			jCheckBoxTransparentBackground = new JCheckBox();
			jCheckBoxTransparentBackground.setText("Transparent background");
		}
		return jCheckBoxTransparentBackground;
	}
	
	/**
	 * This method initializes jButtonStyle	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStyle() {
		if (jButtonStyle == null) {
			jButtonStyle = new JButton();
			jButtonStyle.setText("Style...");
			jButtonStyle.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JDialogFontChooser dlg =new JDialogFontChooser( null, true, JDialogFontChooser.Role.FONT_STYLE_ONLY );
					
					if ( labelsListShape != null && style != null ) {
						dlg.applyStyle( style );
						dlg.applyValuesToUI();
					}
					
					dlg.setVisible( true );
					
					if ( dlg.isOkPressed() ) {
						style =dlg.getStyle();
					}
					
					dlg.dispose();
				}
			});
		}
		return jButtonStyle;
	}
	
	public boolean isOkPressed() {
		return okPressed;
	}
	
	public TextStyle getStyle() {
		return style;
	}
	/**
	 * This method initializes jCheckBoxVisible	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	public JCheckBox getJCheckBoxVisible() {
		if (jCheckBoxVisible == null) {
			jCheckBoxVisible = new JCheckBox();
			jCheckBoxVisible.setText("Visible");
		}
		return jCheckBoxVisible;
	}
	
 }  
